<?php
/**
 * The Joomla data model as scottlib slObject classes.
 * @author Aaron Collegeman aaroncollegeman.com
 * @version 0.1.1-BETA
 * 
 * Copyright (C)2008 Collegeman.net, LLC.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 
 */

/**
 * josMenuLink controls the administrative menus in the back-end, as well as
 * the availability of Menu Item types specific to your custom components.
 * @table(jos_components) 
 */
class josMenuLink extends slObject {
	
	/** @id */
	public $id;
	
	/** @maxLength(50) */
	public $name;
	
	/** @maxLength(255) */
	public $link = '';
	
	/** 
	 * @integer
	 * @fieldName(menuid) 
	 */
	public $menuId = 0;
	
	/** @integer */
	public $parent = 0;
	
	/** 
	 * @maxLength(255)
	 * @fieldName(admin_menu_link) 
	 */
	public $adminMenuLink = '';
	
	/** 
	 * @maxLength(255)
	 * @fieldName(admin_menu_alt) 
	 */
	public $adminMenuAlt = '';
	
	/**
	 * @maxLength(255)
	 * @fieldName(admin_menu_img)
	 */
	public $adminMenuImg = 's/ThemeOffice/component.png';
	
	/** @integer */
	public $ordering = 0;
	
	public $option = '';
	
	/** @boolean */
	public $enabled = 1;
	
	/** @integer */
	public $iscore = 1;
	
	static function deleteAllForComponent($link) {
		$conn = slMySqlConn::instance();
		$parentId = $conn->val(prepare("SELECT id FROM `jos_components` WHERE `option` = ? ORDER BY `ordering` LIMIT 1", $link));
		$conn->execute(prepare("DELETE FROM `jos_components` WHERE `option` = ? OR `parent` = %d", array($link, $parentId)));
	}
	
}

/**
 * josSection allows you to create, edit, and delete article sections in the Joomla CMS.
 * @table(jos_sections) 
 */
class josSection extends slObject {
	
	/** @id */
	public $id;
	
	/** 
	 * @maxLength(255) 
 	 * @notNull
	 */
	public $title;
	
	/** 
	 * @maxLength(255) 
	 * @notNull
	 */
	public $name = '';
	
	/** 
	 * @maxLength(255) 
	 * @notNull
	 */
	public $alias = '';
	
	/**
 	 * @text
	 * @notNull
	 */
	public $image = '';
	
	/**
 	 * @maxLength(50)
	 * @notNull
	 */
	public $scope = 'content';
	
	/**
	 * @maxLength(30)
	 * @notNull
	 * @fieldName(image_position)
	 */
	public $imagePosition = 'left';
	
	/**
	 * @text
	 * @notNull
	 */
	public $description = '';
	
	/**
	 * @boolean
	 * @notNull
	 * @fieldName(checked_out)
	 */
	public $checkedOut = false;
	
	/**
	 * @date
	 * @notNull
	 * @fieldName(checked_out_time)
	 */
	public $checkedOutTime = 0;
	
	/**
	 * @integer
	 * @notNull
	 */
	public $ordering = 1;
		
	/**
	 * @integer
	 * @notNull
	 */
	public $access = 0;
	
	/**
	 * @integer
	 * @notNull
	 */
	public $count = 0;
	
	/**
 	 * @text
	 * @notNull
	 */
	public $params = '';
	
	/**
	 * @boolean
	 * @notNull
	 */
	public $published = true;
	
	static function getUniqueSectionName($startWith) {
		$conn = slMySqlConn::instance();
		
		$next = $startWith;
		$i = 0;
		while ($conn->val(prepare("SELECT COUNT(*) FROM jos_sections WHERE title=%s", $next))) {
			$i++;
			$next = $startWith.$i;	
		}
		return $next;
	}
	
	static function createNew($title, $description = '') {
		$section = new josSection();
		$section->title = $title;
		$section->alias = strtolower(preg_replace('/[^a-z0-9]/i', '_', $title));
		$section->description = $description;
		$section->save();
		return $section;
	}
	
	static function createNewCategory(josSection &$section, $title, $description = '') {
		$category = new josCategory();
		$category->title = $title;
		$category->description = $description;
		$category->section = $section->id;
		$category->save();
		return $category;
	}
	
}

/**
 * josCategory allows you to create, edit, and delete article categories in the Joomla CMS.
 * @table(jos_categories) 
 */
class josCategory extends slObject {
	
	/** @id */
	public $id;
	
	/** 
 	 * The id of the parent category, if applicable.
	 * @integer
	 * @notNull
	 * @fieldName(parent_id)
	 */
	public $parentId = 0;
	
	
	/** 
	 * @maxLength(255) 
 	 * @notNull
	 */
	public $title;
	
	/** 
	 * @maxLength(255) 
	 * @notNull
	 */
	public $name = '';
	
	/** 
	 * @maxLength(255) 
	 * @notNull
	 */
	public $alias = '';
	
	/**
 	 * @text
	 * @notNull
	 */
	public $image = '';
	
	/**
	 * The id of the section in which this category lives
	 * @maxLength(50)
	 * @notNull
	 */
	public $section = '';
	
	/**
	 * @maxLength(30)
	 * @notNull
	 * @fieldName(image_position)
	 */
	public $imagePosition = 'left';
	
	/**
	 * @text
	 * @notNull
	 */
	public $description = '';
	
	/**
	 * @boolean
	 * @notNull
	 * @fieldName(checked_out)
	 */
	public $checkedOut = false;
	
	/**
	 * @date
	 * @notNull
	 * @fieldName(checked_out_time)
	 */
	public $checkedOutTime = 0;
	
	/**
	 * @maxLength(50)
	 */
	public $editor = null;
	
	/**
	 * @integer
	 * @notNull
	 */
	public $ordering = 1;
		
	/**
	 * @integer
	 * @notNull
	 */
	public $access = 0;
	
	/**
	 * @integer
	 * @notNull
	 */
	public $count = 0;
	
	/**
 	 * @text
	 * @notNull
	 */
	public $params = '';
	
	/**
	 * @boolean
	 * @notNull
	 */
	public $published = true;
	
	static function getUniqueCategoryName($startWith) {
		$conn = slMySqlConn::instance();
		
		$next = $startWith;
		$i = 0;
		while ($conn->val(prepare("SELECT COUNT(*) FROM jos_categories WHERE title=%s", $next))) {
			$i++;
			$next = $startWith.$i;	
		}
		return $next;
	}
	
}


/**
 * josMenu allows you to create, edit, and delete menu items, as well as create, edit and delete menu types (the menus themselves) in the Joomla CMS.
 * @table(jos_menu) 
 */
class josMenu extends slObject {

	/** @id */
	public $id;
	
	/** 
	 * Foreign key: jos_menu_types.id
	 * @maxLength(75) 
	 * @fieldName(menutype)
	 */
	public $menuType = null;
	
	/**
  	 * @maxLength(255)
	 */
	public $name = null;
	
	/**
	 * @maxLength(255)
	 * @notNull
	 */
	public $alias = '';
	
	/**
	 * @text
	 */
	public $link = 'index.php?option=com_content&view=article&id=0';
	
	/**
	 * @maxLength(50)
	 * @notNull
	 */
	public $type = 'component';
	
	/**
	 * @bool
	 */
	public $published = true;
	
	/**
	 * @integer
	 */
	public $parent = 0;
	
	/**
	 * @integer
	 */
	public $componentId = 20;
	
	/**
	 * @integer
	 */
	public $sublevel = 0;
	
	/**
	 * @integer
	 */
	public $ordering = 0;

	/**
 	 * @bool
	 * @notNull
	 * @fieldName(checked_out)
	 */
	public $checkedOut = false;
	
	/**
	 * @date
	 * @fieldName(checked_out_time)
	 */
	public $checkedOutTime = 0;
	
	/**
	 * @integer
	 * @fieldName(pollid);
	 */
	public $pollId = 0;
	
	/**
	 * @bool
	 */
	public $browserNav = false;
	
	/**
	 * @bool
	 */
	public $access = false;
	
	/**
	 * @bool
	 */
	public $utaccess = false;
	
	/**
	 * @text
	 */
	public $params = '';
	
	/**
	 * @integer
 	 */
	public $lft = 0;
	
	/**
 	 * @integer
	 */
	public $rgt = 0;
	
	/**
	 * When true, indicates the home page - the default menu item loaded when no other parameters are specified.
     * @bool
	 */
	public $home = false;
	
	static function addType($menuType, $title, $description) { }
	
	static function updateType($id, $menuType, $title, $description) { }
	
	static function deleteType($id_or_menutype) { }
	
	static function nextHighestOrderingValue($menuType) {
		$highest = val("SELECT ordering FROM jos_menu WHERE menutype='hoa-subsites' ORDER BY ordering DESC LIMIT 1");
		return ($highest == null) ? 1 : ++$highest;
	}
	
	static function findByMenuType($menuType) {
		$menu = new josMenu();
		return $menu->select("WHERE menutype=%s", $menuType);
	}

	static function loadByMenuType($menuType) {
		$menus = self::findByMenuType($menuType);
		return count($menus) ? $menus[0] : null;	
	}
	
}

